package com.kiln.juxingqs.dialog

import android.view.View
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.MutableLiveData
import client.global.commlib.network.data.JsonResult
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.GetBathRetAdapter
import com.kiln.juxingqs.api.bean.GetBathRet
import com.kiln.juxingqs.databinding.LayoutBathdialogBinding
import com.kiln.juxingqs.ui.viewmodel.MiddleVm
import com.tzstore.oo.dialog.BottomBindingDialog
import java.util.*

open class BathDialog(private val context: FragmentActivity) :
    BottomBindingDialog<LayoutBathdialogBinding>(context) {

    var okTxt: String = "确定"
    var cancleTxt: String = "取消"
    var ok: ((b: GetBathRet.Bath) -> Unit)? = null
    val getBathRet = MutableLiveData<JsonResult<GetBathRet>>()
    val pageOne = MutableLiveData<Int>(0)
    val isOneLoading = MutableLiveData<Boolean>(false)

    lateinit var vm: MiddleVm

    val adapter: GetBathRetAdapter by lazy {
        GetBathRetAdapter(context).apply {
            ok = {
                this@BathDialog.ok?.invoke(
                    it
                )
                cancel()
            }
        }
    }

    override fun getV(): Int {
        return R.layout.layout_bathdialog
    }

    //挂卖获取批次
    fun holdBath() {
        if (isOneLoading.value!!)
            return
        vm.hold({
            vm.server.sell_bath(pageOne.value!!)
        }, {
            isOneLoading.value = true
            pageOne.value = pageOne.value!! + 1
        }, success = {
            getBathRet.value = it
        }, complete = {
            isOneLoading.value = false
        })
    }

    override fun postBinding() {
        binding.event = this

        setCanceledOnTouchOutside(true)

        binding.recyclerView.apply {
            adapter = this@BathDialog.adapter
            onLoadMore = {
                if (this@BathDialog.adapter.hasMoreState.value!! && !isOneLoading.value!!) {
                    holdBath()
                }
            }
        }
        binding.emptyLayout.setOnClickListener {
            holdBath()
        }

        getBathRet.observe(context, {
            it?.let {
                if (it.isSuccess) {
                    val list =
                        it.data?.bath_list as MutableList<GetBathRet.Bath?>?
                    if (pageOne.value == 1) {
                        adapter.submitList(list)
                    } else
                        adapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 20) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    if (adapter.itemCount == 0)
                        binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })

        holdBath()
//        bath_list?.let {
//            if (it.size > 0) {
//                for (b in it) {
//                    val itemBinding = DataBindingUtil.inflate<LayoutBathitemBinding>(
//                        LayoutInflater.from(context),
//                        R.layout.layout_bathitem,
//                        binding.bathWrap,
//                        false
//                    ).apply {
//                        bean = b
//                        event = this@BathDialog
//                    }
//                    binding.bathWrap.addView(itemBinding.root)
//                }
//
//            }
//        }
    }
}